﻿#include "fastorderdlg.h"
#include "ui_fastorderdlg.h"
#include"ufxconfig.h"
#include<QMessageBox>


FastInfo::FastInfo(QString keyString,    int    bs,    int PriceType,    int     OverPrice,    int ActionType):
  keyString(keyString),
  bs(bs),
  PriceType(PriceType),
  OverPrice(OverPrice),
  ActionType(ActionType)
{

}

FastOrderDlg::FastOrderDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FastOrderDlg)
{
    ui->setupUi(this);
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem(QStringLiteral("快捷键")));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem(QStringLiteral("买卖")));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem(QStringLiteral("价格")));
    ui->tableWidget->setHorizontalHeaderItem(3,new QTableWidgetItem(QStringLiteral("超价")));
    ui->tableWidget->setHorizontalHeaderItem(4,new QTableWidgetItem(QStringLiteral("动作")));
    ui->tableWidget->setColumnWidth(0,80);
    ui->tableWidget->setColumnWidth(1,50);
    ui->tableWidget->setColumnWidth(2,80);
    ui->tableWidget->setColumnWidth(3,50);
    ui->tableWidget->setColumnWidth(4,150);
    ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->setRowCount(12);

    for(int i = 0; i < 12; i++){
        QComboBox* m_BSCombox = new QComboBox();
        m_BSCombox->addItem(QStringLiteral("买"));//0
        m_BSCombox->addItem(QStringLiteral("卖"));//1

        QComboBox* m_PriceCombox = new QComboBox();
        m_PriceCombox->addItem(QStringLiteral("最新价"));//0
        m_PriceCombox->addItem(QStringLiteral("买一价"));//1
        m_PriceCombox->addItem(QStringLiteral("卖一价"));//2
        m_PriceCombox->addItem(QStringLiteral("市场价"));//3

        QComboBox* m_OrderTypeCombox = new QComboBox();
        m_OrderTypeCombox->addItem(QStringLiteral("发出报单(带确认)"));//0
        m_OrderTypeCombox->addItem(QStringLiteral("发出报单(不带确认)"));//1
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标不动"));//2
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标到合约代码"));//3
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标到买卖"));//4
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标到开平"));//5
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标到数量"));//6
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标到价格"));//7
        m_OrderTypeCombox->addItem(QStringLiteral("填单后光标到下单按钮"));//8

        QComboBox* m_KeyCombox = new QComboBox();
        m_KeyCombox->addItem(QStringLiteral("1"));//0
        m_KeyCombox->addItem(QStringLiteral("2"));//1
        m_KeyCombox->addItem(QStringLiteral("3"));
        m_KeyCombox->addItem(QStringLiteral("4"));
        m_KeyCombox->addItem(QStringLiteral("5"));
        m_KeyCombox->addItem(QStringLiteral("6"));
        m_KeyCombox->addItem(QStringLiteral("7"));
        m_KeyCombox->addItem(QStringLiteral("8"));
        m_KeyCombox->addItem(QStringLiteral("9"));
        m_KeyCombox->addItem(QStringLiteral("0"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 1"));//10
        m_KeyCombox->addItem(QStringLiteral("小键盘 2"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 3"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 4"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 5"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 6"));//15
        m_KeyCombox->addItem(QStringLiteral("小键盘 7"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 8"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 9"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 0"));
        m_KeyCombox->addItem(QStringLiteral("A"));//20
        m_KeyCombox->addItem(QStringLiteral("B"));
        m_KeyCombox->addItem(QStringLiteral("C"));
        m_KeyCombox->addItem(QStringLiteral("D"));
        m_KeyCombox->addItem(QStringLiteral("E"));
        m_KeyCombox->addItem(QStringLiteral("F"));
        m_KeyCombox->addItem(QStringLiteral("G"));
        m_KeyCombox->addItem(QStringLiteral("H"));
        m_KeyCombox->addItem(QStringLiteral("I"));
        m_KeyCombox->addItem(QStringLiteral("J"));
        m_KeyCombox->addItem(QStringLiteral("K"));
        m_KeyCombox->addItem(QStringLiteral("L"));
        m_KeyCombox->addItem(QStringLiteral("M"));
        m_KeyCombox->addItem(QStringLiteral("N"));
        m_KeyCombox->addItem(QStringLiteral("O"));
        m_KeyCombox->addItem(QStringLiteral("P"));
        m_KeyCombox->addItem(QStringLiteral("Q"));
        m_KeyCombox->addItem(QStringLiteral("R"));
        m_KeyCombox->addItem(QStringLiteral("S"));
        m_KeyCombox->addItem(QStringLiteral("T"));
        m_KeyCombox->addItem(QStringLiteral("U"));
        m_KeyCombox->addItem(QStringLiteral("V"));
        m_KeyCombox->addItem(QStringLiteral("W"));
        m_KeyCombox->addItem(QStringLiteral("X"));
        m_KeyCombox->addItem(QStringLiteral("Y"));
        m_KeyCombox->addItem(QStringLiteral("Z"));
        m_KeyCombox->addItem(QStringLiteral("F1"));
        m_KeyCombox->addItem(QStringLiteral("F2"));
        m_KeyCombox->addItem(QStringLiteral("F3"));
        m_KeyCombox->addItem(QStringLiteral("F4"));
        m_KeyCombox->addItem(QStringLiteral("F5"));
        m_KeyCombox->addItem(QStringLiteral("F6"));
        m_KeyCombox->addItem(QStringLiteral("F7"));
        m_KeyCombox->addItem(QStringLiteral("F8"));
        m_KeyCombox->addItem(QStringLiteral("F9"));
        m_KeyCombox->addItem(QStringLiteral("F10"));
        m_KeyCombox->addItem(QStringLiteral("F11"));
        m_KeyCombox->addItem(QStringLiteral("F12"));
        m_KeyCombox->addItem(QStringLiteral("空格键"));
        m_KeyCombox->addItem(QStringLiteral("回车键"));
        m_KeyCombox->addItem(QStringLiteral("Alt键"));
        m_KeyCombox->addItem(QStringLiteral("Ctrl键"));
        m_KeyCombox->addItem(QStringLiteral("↑"));
        m_KeyCombox->addItem(QStringLiteral("↓"));
        m_KeyCombox->addItem(QStringLiteral("←"));
        m_KeyCombox->addItem(QStringLiteral("→"));
        m_KeyCombox->addItem(QStringLiteral("减号"));
        m_KeyCombox->addItem(QStringLiteral("加号"));
        m_KeyCombox->addItem(QStringLiteral("等号"));
        m_KeyCombox->addItem(QStringLiteral("乘号"));
        m_KeyCombox->addItem(QStringLiteral("除号"));
        m_KeyCombox->addItem(QStringLiteral("删除键"));
        m_KeyCombox->addItem(QStringLiteral("插入键"));

        ui->tableWidget->setCellWidget(i, 0, (QWidget*)m_KeyCombox);
        ui->tableWidget->setCellWidget(i, 1, (QWidget*)m_BSCombox);
        ui->tableWidget->setCellWidget(i, 2, (QWidget*)m_PriceCombox);
        ui->tableWidget->setCellWidget(i, 4, (QWidget*)m_OrderTypeCombox);

        connect(m_KeyCombox,SIGNAL(currentTextChanged(QString)),this,SLOT(SlotCurrentKeyChanged(QString)));
    }

    //从配置文件中读取预设内容
    QString value = UFXConfig::getInstance()->GetString("Default","FastOrder","");
    if(value != 0){
        QStringList list1 = value.split("|");
        if(list1.size() == 12){
            for(int i = 0;i<12; i++){
                QStringList list2 = list1[i].split(",");
                if(list2.size()==5){
                    ((QComboBox*)ui->tableWidget->cellWidget(i,0))->setCurrentText(list2[0]);
                    ((QComboBox*)ui->tableWidget->cellWidget(i,1))->setCurrentIndex(list2[1].toInt());
                    ((QComboBox*)ui->tableWidget->cellWidget(i,2))->setCurrentIndex(list2[2].toInt());
                    ui->tableWidget->setItem(i,3,new QTableWidgetItem(list2[3]));
                    ((QComboBox*)ui->tableWidget->cellWidget(i,4))->setCurrentIndex(list2[4].toInt());

                    FastInfo* pFastInfo = new FastInfo(list2[0],list2[1].toInt(),list2[2].toInt(),list2[3].toInt(),list2[4].toInt());
                    m_FastInfos.insert(list2[0],pFastInfo);
                }
            }
        }
    }

    QString FuncFastOrderFlg = UFXConfig::getInstance()->GetString("FuncFlag","FuncFastOrderFlg","1");
    if(FuncFastOrderFlg == "1"){
        ui->FuncFastOrderFlg->setChecked(true);
    }else{
        ui->FuncFastOrderFlg->setChecked(false);
    }
}

FastOrderDlg::~FastOrderDlg()
{
    delete ui;
}
void FastOrderDlg::SlotJudgeFastKeyString(QString msg){
    QMessageBox::critical(this,QString("Error"),msg);
}
void FastOrderDlg::SlotCurrentKeyChanged(QString key){
    int iCnt = 0;
    for(int i = 0; i < 12; i++){
        QString keyString = ((QComboBox*)ui->tableWidget->cellWidget(i,0))->currentText();
        if(key == keyString){
            iCnt++;
        }
    }
    if(iCnt > 1){
        QString msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 键盘快速下单 中使用! ");
        QMessageBox::critical(this,QString("Error"),msg);
        return;
    }else{
        emit SigJudgeFastKeyString(key);
    }
}
void FastOrderDlg::OKButtonClicked(){
    m_FastInfos.clear();
    //将设置好的快捷键保存到配置文件中
    QString value = "";
    int cnt = ui->tableWidget->rowCount();
    for(int i =0; i <cnt; i++){
        QString keyStr = ((QComboBox*)ui->tableWidget->cellWidget(i,0))->currentText();
        int bs  = ((QComboBox*)ui->tableWidget->cellWidget(i,1))->currentIndex();
        int pricetype = ((QComboBox*)ui->tableWidget->cellWidget(i,2))->currentIndex();
        QString overprice = ui->tableWidget->item(i,3)->text();
        int ordertype = ((QComboBox*)ui->tableWidget->cellWidget(i,4))->currentIndex();
        QString tmp = keyStr +"," +QString::number(bs) +"," +QString::number(pricetype) +"," +overprice +"," +QString::number(ordertype);
        if(i == 0){
            value = tmp;
        }else{
            value = value +"|"+ tmp;
        }
        FastInfo* pFastInfo = new FastInfo(keyStr,bs,pricetype,overprice.toInt(),ordertype);
        m_FastInfos.insert(keyStr,pFastInfo);
    }
    UFXConfig::getInstance()->SetString("Default","FastOrder",value.toStdString().c_str());

    if(ui->FuncFastOrderFlg->isChecked()){
        UFXConfig::getInstance()->SetString("FuncFlag","FuncFastOrderFlg","1");
    }else{
        UFXConfig::getInstance()->SetString("FuncFlag","FuncFastOrderFlg","0");
    }
}
bool FastOrderDlg::JudgeKeyStringFlg(QString key){
    return m_FastInfos.contains(key);
}
FastInfo* FastOrderDlg::GetFastInfo(QString key){
    if(m_FastInfos.contains(key)){
        return m_FastInfos[key];
    }
    return NULL;
}
bool FastOrderDlg::GetFastOrderFlg(){
    if(ui->FuncFastOrderFlg->checkState() == Qt::Checked){
        return true;
    }
    return false;
}
